import { Component, OnInit } from '@angular/core';
import { AbstractControl, FormControl, FormGroup, Validators } from '@angular/forms';
import { Router } from '@angular/router';
import { MessageService } from 'src/app/service/message.service';
import { UserBindService } from 'src/app/service/user-bind.service';
import { formValid } from 'src/app/utils/form-valid';

@Component({
  selector: 'app-user-bind-add',
  templateUrl: './user-bind-add.component.html',
  styleUrls: ['./user-bind-add.component.scss']
})
export class UserBindAddComponent implements OnInit {

  constructor(
    private userBind: UserBindService,
    private message: MessageService,
    private router: Router
  ) { }

  validateForm = new FormGroup({
    userBindUID: new FormControl('', [Validators.required]),
    userName: new FormControl('', [Validators.required]),
    phone: new FormControl(''),
    checkCode: new FormControl(''),
  })

  ngOnInit(): void {
  }

  formBtnLoading = false
  submit(){
    if(!formValid(this.validateForm)){
      return
    }
    
    this.formBtnLoading = true
    const params = this.validateForm.value
    params.userBindUID = params.userBindUID?.toUpperCase()
    this.userBind._addUserBind(this.validateForm.value).subscribe({
      next: (res) => {
        this.message.successModal('提交成功, 点击确认按钮返回', () => {
          this.router.navigate(['/me/user-bind/list'])
        })

        this.formBtnLoading = false
      },
      error: (error) => {
        this.formBtnLoading = false
        this.message.createErrorMessage(error)
      }
    })
  }
  cancel(){
    this.message.confirmModal('确认重置表单并放回上一页吗', () => {
      this.router.navigate(['/me/user-bind/list'])
    })
  }

  
  public get userBindUID() {
    return this.validateForm.get('userBindUID')
  }
  public get userName() {
    return this.validateForm.get('userName')
  }
  public get phone() {
    return this.validateForm.get('phone')
  }
  public get checkCode() {
    return this.validateForm.get('checkCode')
  }
  

}
